<!-- eslint-disable prettier/prettier -->
<template>
  <div class="management">
    <div class="manage-header">
      <div class="manage-left">
        <div class="manage-title">值班表管理</div>
        <div class="manage-tip">
          <ExclamationCircleOutlined
            class="icon-excla"
          />月底会自动归档已过期的值班表
        </div>
      </div>
      <div class="manage-button">
        <a-button class="addBtn" type="primary" @click="onCreated"
          >新建值班表</a-button
        >
        <a-button class="addBtn" type="normal" @click="onSearch"
          >归档值班表查看</a-button
        >
      </div>
    </div>
    <div class="manage-content" v-if="rotaList.length > 0">
      <div class="content-item" v-for="(item, index) of rotaList" :key="index" @click="onEdit(item)">
        <div class="item-left">
          <svg-icon icon-class="location" class="icon-location" />
        </div>
        <div class="item-right">
          <div class="item-title">{{ item.rotaName }}</div>
          <div class="item-date">{{ item.rotaBegin + ' - ' + item.rotaEnd }}</div>
          <div class="item-button">
            <span @click="onEdit(item)">查看及编辑</span>
            <span @click.stop.prevent="onFile(item)" v-if="item.fileFlg == 0">归档值班表</span>
          </div>
        </div>
      </div>
    </div>

    <div class="manage-footer" v-if="rotaList.length > 0">
      <span>共{{ rec.pager.total }}条数据</span>
      <a-pagination
        v-model:current="rec.pager.pageNum"
        v-model:pageSize="rec.pager.pageSize"
        show-size-changer
        show-quick-jumper
        :total="rec.pager.total"
        @change="onChange"
        @showSizeChange="onShowSizeChange"
        :pageSizeOptions="['10', '20', '40', '100']"
      />
    </div>

    <div>
      <a-empty v-if="rotaList.length == 0"/>
    </div>

    <a-modal
      v-model:visible="visible"
      title="新建值班表"
      @ok="handleOk"
      :destroyOnClose="true"
      :maskClosable="false"
      @cancel="handleCancel"
    >
      <template #footer>
        <div style="padding-bottom: 20px">
          <a-button key="back" @click="handleCancel">取消</a-button>
          <a-button key="submit" type="primary" @click="handleOk"
            >确认</a-button
          >
        </div>
      </template>
      <a-form
        ref="formRef"
        :model="formState"
        :rules="rules"
        :label-col="labelCol"
        style="margin-top: 20px"
      >
        <a-form-item label="名称" required name="rotaName">
          <a-input v-model:value="formState.rotaName" style="width: 320px"  placeholder="请输入名称" :maxLength="50"/>
        </a-form-item>
        <a-form-item label="时间范围" required name="rotaTime">
          <a-range-picker
            v-model:value="formState.rotaTime"
            :disabled-date="disabledDate"
            type="date"
            style="width: 320px"
          />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>
<!-- eslint-disable prettier/prettier -->
<script setup lang="ts">
import { ref, reactive, createVNode, onMounted } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
import { Modal, message } from "ant-design-vue";
import { getRotaList, addRotaInfo, rotaFile } from "@/service/modules/rota/rota";
import dayjs, { Dayjs } from 'dayjs';
import router from "@/router";

const visible = ref(false);
const rotaList = ref<any>([]);

const rec = ref<any>({
  pager: {
    pageNum: 1,
    pageSize: 10,
    total: 3000,
  },
});

const labelCol = { span: 5 };

const formState: any = reactive({
  rotaName: "",
  rotaTime: undefined,
});
const rules = {
  rotaName: [{ required: true, message: "请输入值班表名称", trigger: "blur" }],
  rotaTime: [
    {
      required: true,
      message: "请选择时间范围",
      trigger: "change",
      type: "array",
    },
  ],
};

const disabledDate = (current: Dayjs) => {
  return current && (current < dayjs().subtract(3, 'year').endOf('day') || current > dayjs().subtract(-1, 'year').endOf('day'));
};

onMounted(() => {
  getList();
});

const getList = () => {
  getRotaList({
    pageNum: rec.value.pager.pageNum,
    pageSize: rec.value.pager.pageSize,
  }).then((res: any) => {
    rotaList.value = res.rows;
    rec.value.pager.total = res.total;
  });
};

const onShowSizeChange = (pageNum: number, pageSize: number) => {
  rec.value.pager.pageNum = pageNum;
  rec.value.pager.pageSize = pageSize;
  getList();
};

const onChange = (pageNum: number) => {
  rec.value.pager.pageNum = pageNum;
  getList();
};

const onCreated = () => {
  visible.value = true;
};

const formRef = ref<any>(null);
const handleOk = () => {
  formRef.value.validate().then(() => {
    addRotaInfo({
      rotaName: formState.rotaName,
      rotaBegin: formState.rotaTime[0],
      rotaEnd: formState.rotaTime[1],
    }).then((res: any) => {
      if (res.code === 0) {
        message.success("新建成功！");
        formState.rotaName = null;
        formState.rotaTime = null;
        visible.value = false;
        rec.value.pager.pageNum = 1;
        getList();
      } else {
        message.error(res.msg)
      }
    });
  });
};

const handleCancel = () => {
  formRef.value.resetFields();
  visible.value = false;
};

const onSearch = () => {
  router.push("/rota/search");
};

const onEdit = (item: any) => {
  router.push({
    path: "/rota/edit",
    query: { i: item.id, rotaBegin: item.rotaBegin, rotaEnd: item.rotaEnd },
  });
};

const onFile = (data: any) => {
  Modal.confirm({
    width: "540px",
    title: "确定归档该值班表？",
    content:
      "归档后的值班表，值班老师无法查看。如需再用，可在归档列表中取消归档。确认归档吗？",
    icon: createVNode(ExclamationCircleOutlined),
    async onOk() {
      rotaFile(data.id).then((res: any) => {
        if (res.code === 0) {
          message.success("归档成功！");
          getList();
        } else {
          message.error(res.msg)
        }
      })
    },
    onCancel() {
      console.log("Cancel");
    },
    class: "test",
  });
};
</script>
<style scoped lang="less">
.management {
  width: 100%;
  height: 100%;
  background: #ffffff;
  border-radius: 10px;
  padding: 0px 24px;
  position: relative;
}
.manage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.manage-left {
  display: flex;
  align-items: center;
}
.manage-button {
  display: flex;
  align-items: center;
}

.manage-title {
  width: 80px;
  height: 64px;
  font-size: 16px;
  font-family: Microsoft YaHei UI, Microsoft YaHei UI-600;
  font-weight: 600;
  text-align: left;
  color: #303233;
  line-height: 64px;
  margin-right: 26.17px;
}

.icon-excla {
  font-size: 11.68px;
  margin-right: 5px;
}

.manage-tip {
  width: 206px;
  height: 18px;
  font-size: 14px;
  font-family: Microsoft YaHei UI, Microsoft YaHei UI-400;
  font-weight: 400;
  text-align: LEFT;
  color: #aaaeb2;
  line-height: 16px;
  letter-spacing: 0.07px;
  white-space: pre;
  display: flex;
  align-items: center;
}

.manage-content {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  max-height: 80%;
  overflow-y: auto;
  &::-webkit-scrollbar {
    width: 0px;
  }
}
.content-item {
  width: 31%;
  height: 124px;
  border: 1px solid #ebecf4;
  border-radius: 10px;
  margin-right: 16px;
  margin-bottom: 16px;
  padding: 26px 20px;
  display: flex;
  cursor: pointer;
  &:hover {
    width: 31%;
    height: 124px;
    background: #ffffff;
    border: 1px solid #3e77fa;
    border-radius: 10px;
    box-shadow: 0px 6px 10px 0px rgba(13, 61, 172, 0.06);
  }
  .item-right {
    width: 80%;
  }
}

.icon-location {
  width: 36px;
  height: 36px;
  margin-right: 15px;
}

.item-title {
  width: 80%;
  height: 26px;
  font-size: 16px;
  font-family: Microsoft YaHei UI, Microsoft YaHei UI-600;
  font-weight: 600;
  text-align: LEFT;
  color: #303233;
  line-height: 26px;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.item-date {
  width: 193px;
  height: 18px;
  font-size: 14px;
  font-family: Arial, Arial-400;
  font-weight: 400;
  text-align: left;
  color: #aaaeb2;
  line-height: 16px;
  margin-top: 4px;
}
.item-button {
  margin-top: 16px;
  height: 20px;
  font-size: 14px;
  font-family: Microsoft YaHei UI, Microsoft YaHei UI-400;
  font-weight: 400;
  text-align: LEFT;
  color: #3e77fa;
  line-height: 20px;
  letter-spacing: 0.07px;
  display: flex;
  span {
    width: 73px;
    height: 20px;
    margin-right: 16px;
    cursor: pointer;
  }
}
.manage-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 95%;
  height: 6%;
  position: absolute;
  bottom: 0;
  padding-bottom: 20px;
}
</style>
